<?php
defined('IN_XLP') or exit('Access Denied!');

/*
 * 众筹活动后台
 */
class fundAction extends commonAction {

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
    }

    /*
     * 各小区众筹情况
     */
    public function index() {
        $page  = empty($_GET['page']) ? 1 : intval($_GET['page']);
        $limit = ($page - 1)*20;

        $fund_list = M('fund_village','db_event')
            ->field('`village_id`,`partin_num`,`donate_num`,`donate_total`,`fund_target`,`fund_total`,`fund_content`,`passed`')
            ->order('`id` desc')
            ->limit("$limit,20")
            ->findAll();
        $total = M('fund_village','db_event')->getTotal();

        $vid_arr = array();
        foreach ($fund_list as $v) {
            $vid_arr[] = $v['village_id'];
        }
        $vids = implode(',', $vid_arr);
        if(empty($vids)) $vids = 0;

        $village = M('village')
            ->field('`vid`,`title`')
            ->where("`vid` in ($vids)")
            ->findAll();

        $temp_arr = array();
        foreach ($village as $v) {
            $temp_arr[$v['vid']] = $v['title'];
        }

        foreach ($fund_list as $k => $v) {
            $fund_list[$k]['village'] = $temp_arr[$v['village_id']];
            $fund_list[$k]['status']  = $v['passed'] ? '已通过' : '<span class="red">待审核</span>';
        }

        $this->assign('page', $page);
        $this->assign('total', $total);
        $this->assign('fund_list', $fund_list);
        $this->display();
    }

    /*
     * 申请发起人信息
     */
    public function apply() {
        $page  = empty($_GET['page']) ? 1 : intval($_GET['page']);
        $limit = ($page - 1)*20;

        $apply_list = M('fund_content', 'db_event')
            ->field('`uid`,`village_id`,`wx_account`,`fund_content`,`submit_time`')
            ->order('`village_id` desc')
            ->limit("$limit,20")
            ->findAll();
        $total = M('fund_content','db_event')->getTotal();

        $vid_arr = array();
        foreach ($apply_list as $v) {
            $vid_arr[] = $v['village_id'];
        }
        $vids = implode(',', $vid_arr);
        if(empty($vids)) $vids = 0;

        $village = M('village')
            ->field('`vid`,`title`')
            ->where("`vid` in ($vids)")
            ->findAll();

        $temp_arr = array();
        foreach ($village as $v) {
            $temp_arr[$v['vid']] = $v['title'];
        }
        foreach ($apply_list as $k => $v) {
            $apply_list[$k]['village'] = $temp_arr[$v['village_id']];
            $apply_list[$k]['time']    = date('Y-m-d H:i:s', $v['submit_time']);
        }

        $this->assign('page', $page);
        $this->assign('total', $total);
        $this->assign('apply_list', $apply_list);
        $this->display();
    }

    /*
     * 编辑各小区众筹目标
     */
    public function edit() {
        if(isset($_POST['vid'])) {
            $vid = $this->_postid('vid');
            $result = M('fund_village','db_event')->update(
                array(
                    'fund_target'  => $this->_post('fund_target'),
                    'fund_content' => $this->_post('fund_content'),
                    'passed'       => $this->_postid('passed'),
                ),
                array('village_id'=>$vid)
            );

            $this->JsonReturn('编辑成功', null, 1);
        }

        $vid = $this->_getid('vid');
        $fund_info = M('fund_village','db_event')
            ->field('`village_id`,`fund_target`,`fund_content`,`passed`')
            ->where(array('village_id'=>$vid))
            ->find();
        if(empty($fund_info)) showError('该小区不存在');

        $village = M('village')->field('`title`')->where(array('vid'=>$fund_info['village_id']))->find();
        $fund_info['village'] = $village['title']; 

        $this->assign('fund_info', $fund_info);
        $this->display();
    }

    public function user_partin() {
        $page  = empty($_GET['page']) ? 1 : intval($_GET['page']);
        $limit = ($page - 1)*20;

        $village_id = $this->_getid('vid', 0);

        if(isset($_GET['export'])) {
            $limit = '';
        } else {
            $limit = "$limit,20";
        }

        $user_list = M('fund_clicked', 'db_event')
            ->field('`uid`,`click_time`')
            ->where("`village_id` = $village_id")
            ->order('`click_time` desc')
            ->limit("$limit")
            ->findAll();

        $total = M('fund_clicked', 'db_event')
            ->field('`uid`')
            ->where("`village_id` = $village_id")
            ->count();

        $user_arr = array();
        $users    = array();
        foreach ($user_list as $v) {
            $users[$v['uid']] = $v;
            $user_arr[] = $v['uid'];
        }
        unset($user_list);

        $user_ids = implode(',', $user_arr);
        if(empty($user_ids)) $user_ids = 0;

        $user_info = M('user')
            ->field('`uid`,`username`,`nickname`,`phone`')
            ->where("`uid` in ($user_ids)")
            ->findAll();
        $temp_arr = array();
        foreach ($user_info as $k => $v) {
            $temp_arr[$v['uid']] = array(
                'username' => empty($v['username']) ? '' : $v['username'],
                'nickname' => empty($v['nickname']) ? '' : $v['nickname'],
                'phone'    => empty($v['phone']) ? '' : $v['phone'],
            );
        }
        unset($user_info);

        $user_list = array();
        foreach ($users as $k => $v) {
            $users[$k]['username'] = $temp_arr[$k]['username'];
            $users[$k]['nickname'] = $temp_arr[$k]['nickname'];
            $users[$k]['phone']    = $temp_arr[$k]['phone'];
            $users[$k]['click_time'] = date('Y-m-d H:i:s', $v['click_time']);
            $user_list[] = $users[$k];
        }
        unset($users);

        $village = M('village')
            ->field('`title`')
            ->where("`vid`=$village_id")
            ->find();

        if(isset($_GET['export'])) {
            $this->_exportPartin($user_list, $village['title']);
            exit;
        }

        $this->assign('village', $village['title']);
        $this->assign('vid', $village_id);
        $this->assign('page', $page);
        $this->assign('total', $total);
        $this->assign('users', $user_list);
        $this->display();
    }

    public function user_donate() {
        $page  = empty($_GET['page']) ? 1 : intval($_GET['page']);
        $limit = ($page - 1)*20;

        $village_id = $this->_getid('vid', 0);

        if(isset($_GET['export'])) {
            $limit = '';
        } else {
            $limit = "$limit,20";
        }

        $donate_list = M('fund_donate', 'db_event')
            ->field('`uid`,`donate_sn`,`donate`,`time_pay`')
            ->where(" `village_id` = $village_id AND `time_pay`>0 ")
            ->order('`time_pay` desc')
            ->limit("$limit")
            ->findAll();

        $total = M('fund_donate', 'db_event')
            ->field('`uid`')
            ->where(" `village_id` = $village_id AND `time_pay`>0 ")
            ->count();

        $user_arr = array();
        $users    = array();
        foreach ($donate_list as $v) {
            $users[$v['uid']] = $v;
            $user_arr[] = $v['uid'];
        }
        unset($donate_list);

        $user_ids = implode(',', $user_arr);
        if(empty($user_ids)) $user_ids = 0;

        $user_info = M('user')
            ->field('`uid`,`username`,`nickname`,`phone`')
            ->where("`uid` in ($user_ids)")
            ->findAll();
        $temp_arr = array();
        foreach ($user_info as $k => $v) {
            $temp_arr[$v['uid']] = array(
                'username' => empty($v['username']) ? '' : $v['username'],
                'nickname' => empty($v['nickname']) ? '' : $v['nickname'],
                'phone'    => empty($v['phone']) ? '' : $v['phone'],
            );
        }
        unset($user_info);

        $donate_list = array();
        foreach ($users as $k => $v) {
            $users[$k]['username'] = $temp_arr[$k]['username'];
            $users[$k]['nickname'] = $temp_arr[$k]['nickname'];
            $users[$k]['phone']    = $temp_arr[$k]['phone'];
            $users[$k]['time_pay'] = date('Y-m-d H:i:s', $v['time_pay']);
            $donate_list[] = $users[$k];
        }
        unset($users);

        $village = M('village')
            ->field('`title`')
            ->where("`vid`=$village_id")
            ->find();

        if(isset($_GET['export'])) {
            $this->_exportDonate($donate_list, $village['title']);
            exit;
        }

        $this->assign('village', $village['title']);
        $this->assign('vid', $village_id);
        $this->assign('page', $page);
        $this->assign('total', $total);
        $this->assign('donate', $donate_list);
        $this->display();
    }

    private function _exportPartin($data, $village) {
        $data_title = array('UID','昵称','电话','参与时间');
        $data_list  = array();

        foreach ($data as $k => $v) {
            $data_list[] = array(
                'uid'        => $v['uid'],
                'nickname'   => $v['nickname'],
                'phone'      => "\t".$v['phone'],
                'click_time' => "\t".$v['click_time'],
            );
        }

        load('csv');
        $csv = new csv();
        $csv->write($data_title, $data_list, $village.'参与用户'.date('Y-m-d'));
    }

    private function _exportDonate($data, $village) {
        $data_title = array('UID','昵称','电话','捐款编号','金额','捐款时间');
        $data_list  = array();

        foreach ($data as $k => $v) {
            $data_list[] = array(
                'uid'       => $v['uid'],
                'nickname'  => $v['nickname'],
                'phone'     => "\t".$v['phone'],
                'donate_sn' => $v['donate_sn'],
                'donate'    => priceFormat($v['donate']),
                'time_pay'  => "\t".$v['time_pay'],
            );
        }

        load('csv');
        $csv = new csv();
        $csv->write($data_title, $data_list, $village.'捐款用户'.date('Y-m-d'));
    }

}